package com.wing.order.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.enterprise.feign.EnterpriseClient;
import com.wing.order.model.entity.InventoryJobLabel;
import com.wing.order.model.entity.OrderDeliveryLabel;
import com.wing.order.model.entity.Purchase;
import com.wing.order.model.request.PurchaseForm;
import com.wing.order.model.response.OrderDeliverySignVO;
import com.wing.order.model.response.PurchaseBarCodeVo;
import com.wing.order.model.response.PurchaseVO;
import com.wing.order.service.InventoryJobLabelService;
import com.wing.order.service.OrderDeliveryLabelService;
import com.wing.order.service.OrderDeliverySignService;
import com.wing.order.service.PurchaseService;
import com.wing.platform.feign.PlatformClient;
import com.wing.product.service.ProductPackLabelService;
import com.wing.user.feign.UserClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Validated
@Api(value = "Admin-采购签收", tags = "#/order/purchaseSign")
@RestController("purchaseSignController")
@RequestMapping("order/admin/purchaseSign")
@Slf4j
@RequiredArgsConstructor
public class PurchaseSignController {

    private final PurchaseService purchaseService;

    private final EnterpriseClient enterpriseClient;

    private final OrderDeliverySignService orderDeliverySignService;

    private final InventoryJobLabelService labelService;

//    private final ProductPackLabelService productPackLabelService;


    @ApiOperation(value = "查询所有采购签收列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            , @ApiImplicitParam(name = "sort", value = "排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            , @ApiImplicitParam(name = "order", value = "排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<OrderDeliverySignVO> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , @RequestParam(value = "code", required = false) String code
            , @RequestParam(value = "purchaseCode", required = false) String purchaseCode
            , @RequestParam(value = "status", required = false) String status
    ) {
        //搜索
        Page pg = new Page(page, limit);
        Map queryParam = new HashMap();
        queryParam.put("sort", StrUtil.toUnderlineCase(sort));
        queryParam.put("order", order);
        // 签收编号
        queryParam.put("code", code);
        // 采购编号
        queryParam.put("purchaseCode", purchaseCode);
        // 状态
        queryParam.put("status", status);
        IPage<OrderDeliverySignVO> list = orderDeliverySignService.getPurchaseSignVOPageList(pg, queryParam);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /**
     * 采购签收详情 根据id
     */
    @GetMapping("/getPurchaseSignVOById")
    public JsonResult<OrderDeliverySignVO> getPurchaseSignVOById(@RequestParam("id") Long id) {
        OrderDeliverySignVO vo = orderDeliverySignService.getPurchaseSignVOById(id);
        return JsonResult.success(vo);
    }

    /**
     * 采购签收 根据采购编号 查询采购信息
     */
    @GetMapping("/getPurchaseByCode")
    public JsonResult<PurchaseVO> getPurchaseById(@RequestParam("code") String code) {
        PurchaseVO purchaseVO = purchaseService.getPurchaseByCode(code);

        return JsonResult.success(purchaseVO);
    }

    /**
     * 包装方式
     */
    @GetMapping("/getPackAll")
    public JsonResult getPackAll() {
        JsonResult packAll = enterpriseClient.getProductPackAll();
        if (packAll.getCode() == 200) {
            return JsonResult.success(packAll.getData());
        }
        return JsonResult.fail("getPackAll接口报错");
    }


    /**
     * 包装数量计算
     * @param num 采购数量
     * @param baseRate 换算率(包装方式 数量)
     */
/*    @GetMapping("/calculatePackNum")
    public JsonResult calculatePackNum(@RequestParam("num")Integer num,@RequestParam("baseRate")Integer baseRate){
        if(num!=0){
            Integer packNum = num/baseRate;
            return JsonResult.success(packNum);
        }
        return JsonResult.fail("参数错误");
    }*/

    /**
     * 采购签收
     */
    @PostMapping("/purchaseSign")
    public JsonResult purchaseSign(@RequestBody PurchaseForm purchaseForm) {
        try {
            purchaseService.purchaseSign(purchaseForm);
            return JsonResult.success("签收成功");
        } catch (Exception e) {
            log.error("purchaseSign fail" + e);
            return JsonResult.fail(e.getMessage());
        }
    }

    /**
     * 根据签收id查询 签收单下所有产品的签收条码
     */
    @GetMapping("/getLableBySignId")
    public PageResult<PurchaseBarCodeVo> getLableBySignId(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , @RequestParam("id") Long id) {
        IPage<PurchaseBarCodeVo> list = purchaseService.getLableBySignId(page, limit, id);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    /**
     * 打印条码功能
     */
    @ApiOperation(value = "打印条形码")
    @GetMapping("/getBarCode")
    public JsonResult<String> getBarCode(@RequestParam(value = "code") String code) {
        return enterpriseClient.getBarCode(code);
    }
}
